String clazzTpl({package}) => """
import 'dart:io';
import 'package:dio/io.dart';
import 'package:dio/dio.dart';
import 'package:$package/utils/toast_util.dart';
import 'package:$package/debug/app_debug_model.dart';
import 'package:$package/api/transformer/transformer.dart';
import 'package:$package/widget/base_store_container.dart';
import 'package:$package/api/interceptors/log_interceptors.api.dart';
import 'package:$package/api/interceptors/request_interceptors.api.dart';
import 'package:$package/api/interceptors/response_interceptors.api.dart';

/// Generates Dart code
class HttpManager {
  static final HttpManager _instance = HttpManager._internal();
  static Dio? _dio;
  static HttpClient? _client;

  factory HttpManager({required String host}) => _instance._host(host);

  HttpManager._internal() {
    if (null == _dio) {
      _dio = Dio(BaseOptions(
        contentType: Headers.jsonContentType,
        responseType: ResponseType.json,
        connectTimeout: const Duration(seconds: 30),
        receiveTimeout: const Duration(seconds: 30),
      ));
      _dio!.interceptors.add(RequestInterceptors());
      _dio!.interceptors.add(ResponseInterceptors());
      _dio!.interceptors.add(LogInterceptors());
      _dio!.httpClientAdapter = IOHttpClientAdapter()
        ..onHttpClientCreate = (client) {
          // 设置代理
          String? proxyStr = _checkChangeProxy();
          if (proxyStr != null && proxyStr.isNotEmpty) {
            client.findProxy = (uri) {
              return proxyStr;
            };
          }
          //校验证书
          client.badCertificateCallback =
              (X509Certificate cert, String host, int port) {
            return true;
          };
          _client = client;
          return _client;
        };
    }
  }

  //用于指定特定域名
  HttpManager _host(String host) {
    if (_dio != null && _dio!.options.baseUrl != host) {
      _dio!.options.baseUrl = host;
    }
    return this;
  }

  Future<Response<dynamic>> request<T>(
    String path, {
    dynamic data,
    required Map<String, dynamic> queryParameters,
    required Options options,
    CancelToken? cancelToken,
    ProgressCallback? onSendProgress,
    ProgressCallback? onReceiveProgress,
  }) async {
    if (options.extra != null &&
        options.extra!.containsKey('isLoading') &&
        options.extra!['isLoading']) {
      Toast.showLoading();
    }
    Response resp;
    try {
      _checkChangeProxy();
      _checkChangeHost();
      resp = await _dio!.request(path,
          data: data,
          queryParameters: queryParameters,
          cancelToken: cancelToken,
          options: options,
          onSendProgress: onSendProgress,
          onReceiveProgress: onReceiveProgress);
    } on DioError catch (e) {
      resp = Response(
        data: e,
        requestOptions: e.requestOptions,
        statusCode: e.response?.statusCode,
        statusMessage: e.response?.statusMessage,
        isRedirect: e.response?.isRedirect ?? false,
        redirects: e.response?.redirects ?? [],
        extra: e.response?.extra ?? {},
        headers: e.response?.headers,
      );
    }
    if (resp.data is! DioError &&
        T.toString() != 'dynamic' &&
        T.toString() != 'void') {
      resp.data = await MyTransformer<T>().transformer(resp.data);
    }
    if (resp.data is! DioError &&
        T.toString() == 'dynamic' &&
        resp.data is String) {
      resp.data = await MyTransformer<T>().transformerString(resp.data);
    }
    if (resp.requestOptions.extra.containsKey('isLoading') &&
        resp.requestOptions.extra['isLoading']) {
      Toast.closeLoading();
    }
    // resp.data is DioError or BaseEntity<T> or dynamic
    return resp;
  }

  void _checkChangeHost() {
    // 处理人为切换环境
    final initConfig = StaticStore.store!.state.appConfigState!;
    AppDebugModel? item = initConfig.findAppDebugItem(AppDebugKey.hosts);
    if (item != null && item.childList != null) {
      String selectHost =
          item.childList!.firstWhere((element) => element.isDef).value;
      DataModel? isExists;
      try {
        // 是否包含,处理网络传其他host情况
        isExists = item.childList!.firstWhere(
          (element) => element.value == _dio!.options.baseUrl,
        );
      } catch (_) {}
      if (_dio != null &&
          isExists != null &&
          selectHost != _dio!.options.baseUrl) {
        _dio!.options.baseUrl = selectHost;
      }
    }
  }

  String? _checkChangeProxy() {
    // 设置代理
    final initConfig = StaticStore.store!.state.appConfigState!;
    AppDebugModel? item = initConfig.findAppDebugItem(AppDebugKey.proxy);
    if (item != null &&
        item.desc != null &&
        item.desc!.isNotEmpty &&
        item.port != null &&
        item.port!.isNotEmpty) {
      if (_client != null) {
        _client!.findProxy = (uri) {
          return "PROXY \${item.desc}:\${item.port}";
        };
      }
      return "PROXY \${item.desc}:\${item.port}";
    } else {
      if (_client != null) {
        _client!.findProxy = null;
      }
    }
    return null;
  }
}
""";
